package com.base.cn.platform.os.controller.statistics.base;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.statistics.base.StatisticsDateQueryCondition;
import com.base.cn.platform.os.entity.statistics.base.ZStatisticsOrderBaseCount;
import com.base.cn.platform.os.service.statistics.base.ZStatisticsOrderBaseCountBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 基础统计中的交易基础数据统计Controller
 *
 * @author s.li
 * @date 2019/04/18
 */
@RestController
@RequestMapping("/manage/statistics")
public class ZStatisticsOrderBaseCountController extends BaseController {
    private DecimalFormat decimalFormat = new DecimalFormat("#.##");
    @Autowired
    private ZStatisticsOrderBaseCountBiz zStatisticsOrderBaseCountBiz;

    /**
     *  获取订单统计数据
     * @return Map<String,Object>
     */
    @RequestMapping("/queryOrderBaseCountList")
    public Map<String,Object> queryOrderBaseCountList(@RequestBody StatisticsDateQueryCondition condition){
        //设置时间条件
        Date nowDate =new Date();
        condition.setTimeCondition(nowDate);
        //查询订单数据
        List<ZStatisticsOrderBaseCount> orderBaseCountList = zStatisticsOrderBaseCountBiz.queryOrderBaseCountList(condition);
        //查询订单总和
        List<Date> dateList = condition.getDayList();
        //数据统计总数据量
        ZStatisticsOrderBaseCount sumOrderBaseCount = zStatisticsOrderBaseCountBiz.queryOrderTimeSlotDayCountSum(dateList);

        //付款金额
        double payAmount = sumOrderBaseCount.getPayAmount();
        //付款订单量
        int payOrderCount = sumOrderBaseCount.getPayOrderCount();
        //下单人数
        int orderUserCount = sumOrderBaseCount.getOrderUserCount();
        //付费转化率
        Float payConversionRate;
        //访客数(UV)
        int visitorCount;
        //付款人数
        int payUserCount = sumOrderBaseCount.getPayUserCount();
        //下单量
        int orderCount = sumOrderBaseCount.getOrderCount();
        //下单金额
        double orderAmount = sumOrderBaseCount.getOrderAmount();
        //客单价
        Double averageAmount;
        //下单转化率
        Float orderRate;
        //付款转化率
        Float payRate;
        //平台转化率
        Float userPayRate;

        if(condition.getType()==1){
            visitorCount = orderBaseCountList.get(0).getVisitorCount();
        }else{
            visitorCount = orderBaseCountList.stream().mapToInt(ZStatisticsOrderBaseCount::getVisitorCount).sum();
        }
        payConversionRate = new Float(payUserCount) / new Float(visitorCount);
        if(payConversionRate.isNaN() || payConversionRate.isInfinite()){
            payConversionRate = 0.0f;
        }
        payConversionRate = payConversionRate * 100f;

        averageAmount = payAmount / new Double(payUserCount);
        if(averageAmount.isInfinite() || averageAmount.isNaN()){
            averageAmount = 0.0d;
        }

        orderRate = new Float(sumOrderBaseCount.getOrderCount()) / new Float(visitorCount);
        if(orderRate.isInfinite() || orderRate.isNaN()){
            orderRate = 0.0f;
        }
        payRate = new Float(payUserCount) / new Float(orderCount);
        if(payRate.isNaN() || payRate.isInfinite()){
            payRate = 0.0f;
        }
        userPayRate =  new Float(payUserCount) / new Float(visitorCount);
        if(userPayRate.isInfinite() || userPayRate.isNaN()){
            userPayRate = 0.0f;
        }
        Map<String,Object> resultMap = ResultUtil.SUCCESS(orderBaseCountList);
        resultMap.put("payAmount",payAmount);
        resultMap.put("payOrderCount",payOrderCount);
        resultMap.put("orderUserCount",orderUserCount);
        resultMap.put("payConversionRate",decimalFormat.format(payConversionRate));
        resultMap.put("visitorCount",visitorCount);
        resultMap.put("payUserCount",payUserCount);
        resultMap.put("orderCount",orderCount);
        resultMap.put("orderAmount",orderAmount);
        resultMap.put("averageAmount",decimalFormat.format(averageAmount));
        resultMap.put("orderRate",decimalFormat.format(orderRate*100f));
        resultMap.put("payRate",decimalFormat.format(payRate*100f));
        resultMap.put("userPayRate",decimalFormat.format(userPayRate*100f));
        resultMap.put("type",condition.getType());
        return resultMap;
    }
}
